package com.hnust.controller;

import com.hnust.domain.ClassNameRecord;
import com.hnust.domain.CourseGroup;
import com.hnust.domain.TeacherNameRecord;
import com.hnust.domain.record.*;
import com.hnust.service.CourseGroupService;
import com.hnust.service.TokenService;
import com.hnust.util.Result;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 创建人：张汝往
 * 创建时间：2020/10/7
 * 描述：课程组列表
 */
@CrossOrigin("*")
@RestController
@RequestMapping("/api/courseGroup")
public class CourseGroupController {

    @Autowired
    private TokenService tokenService;

    @Autowired
    private CourseGroupService courseGroupService;


    /**
     * 获取学院所有课程
     * @param token token
     * @param collegeID 学院ID
     * @return
     */
    @GetMapping("/get/courses")
    public Result<List<CourseRecord>> getCourses(
            @RequestParam("token")String token ,
            @RequestParam("collegeId")String collegeID
    ){
        if(tokenService.verifyToken(token,"院管理员")){
            List<CourseRecord> courses=courseGroupService.getCourses(collegeID);
            return new Result<List<CourseRecord>>(1,"获取数据成功",courses);
        }else{
            return new Result<>(0,"没有操作权限,请登录",null);
        }
    }


    /**
     * 获取学院所有班级
     * @param token token
     * @param collegeId 学院ID（输出了5个）
     * @return
     */
    @GetMapping("/get/classes")
    public Result<List<ClassNameRecord>> getClasses(
            @RequestParam("token")String token,
            @RequestParam("collegeId")String collegeId
    ){
        if(tokenService.verifyToken(token,"院管理员")){
            List<ClassNameRecord> classes=courseGroupService.getClasses(collegeId);
            return new Result<List<ClassNameRecord>>(1,"获取数据成功",classes);
        }else{
            return new Result<>(0,"没有操作权限,请登录",null);
        }
    }

    /**
     * 获取学院所有老师
     * @param token token
     * @param collegeId 学院ID
     * @return
     */
    @GetMapping("/get/teachers")
    public Result<List<TeacherNameRecord>> getTeachers(
            @RequestParam("token")String token,
            @RequestParam("collegeId")String collegeId
    ){
        if(tokenService.verifyToken(token,"院管理员")){
            List<TeacherNameRecord> teachers=courseGroupService.getTeachers(collegeId);
            return new Result<List<TeacherNameRecord>>(1,"获取数据成功",teachers);
        }else{
            return new Result<>(0,"没有操作权限,请登录",null);
        }
    }

    /**
     * 查询课程组信息
     * @param token token
     * @param collegeId 学院ID
     * @return
     */
    @GetMapping("/get")
    public Result<List<SimpleCourseGroupInfo>> getSimpleCourseGroupInfo(
            @RequestParam("token")String token,
            @RequestParam("collegeId")String collegeId
    ){
        if(tokenService.verifyToken(token,"院管理员")){
            List<SimpleCourseGroupInfo> simpleCourseGroupInfos=courseGroupService.querySimpleCourseGroup(collegeId);
            return new Result<List<SimpleCourseGroupInfo>>(1,"获取数据成功",simpleCourseGroupInfos);
        }else{
            return new Result<>(0,"没有操作权限,请登录",null);
        }
    }

    /**
     * 查询课程组信息
     * @param map token的搜索条件类
     * @return
     */
//    @PostMapping("/query")
//    public Result<List<SimpleCourseGroupInfo>> querySimpleCourseGroupInfo(
//            @RequestBody Map<String,Object> map
//    ){
//        String token=map.get("token").toString();
//        String collegeId=map.get("collegeId").toString();
//        JSONObject jsonObject=JSONObject.fromObject(map.get("searchCourseGroup"));
//        SearchCourseGroupInfo s=(SearchCourseGroupInfo) JSONObject.toBean(jsonObject, SearchTeacherInfo.class);
//        if(tokenService.verifyToken(token,"院管理员")){
//            List<SimpleCourseGroupInfo> simpleCourseGroupInfos=courseGroupService.querySimpleCourseGroup(collegeId,s);
//            return new Result<List<SimpleCourseGroupInfo>>(1,"获取数据成功",simpleCourseGroupInfos);
//        }else{
//            return new Result<>(0,"没有操作权限,请登录",null);
//        }
//    }

    /**
     * 删除课程组
     * @param token token
     * @param courseGroupId 课程组ID
     * @return
     */

    @GetMapping("/delete")
    public Result<Boolean> deleteCourseGroup(
            @RequestParam("token")String token,
            @RequestParam("courseGroupId")String courseGroupId
    ){
        if(tokenService.verifyToken(token,"院管理员")){
            if(courseGroupService.deleteCourseGroup(courseGroupId)){
                return new Result<>(1,"操作成功",null);
            }else {
                return new Result<>(0,"操作失败",null);
            }
        }else{
            return new Result<>(0,"没有操作权限,请登录",null);
        }
    }

    /**
     * 批量删除课程组
     * @param map token和课程组ID们
     * @return
     */
    @PostMapping("/batchDel")
    public Result<Boolean> batchDeleteCourseGroup(
            @RequestBody Map<String,Object> map
    ){
        String token=map.get("token").toString();
        List<String> courseGroupIds=(List<String>) JSONArray.fromObject(map.get("courseGroupIds"));
        if(tokenService.verifyToken(token,"院管理员")){
            if(courseGroupService.batchDeleteCourseGroup(courseGroupIds)){
                return new Result<>(1,"操作成功",null);
            }else{
                return new Result<>(0,"操作失败",null);
            }
        }else{
            return new Result<>(0,"没有操作权限,请登录",null);
        }
    }


    /**
     * 修改课程组信息
     * @param map 课程组信息和token
     * @return
     */
    @PostMapping("/modify")
    public Result<Boolean> modifyCourseGroup(
            @RequestBody Map<String,Object> map
    ){
        String token=map.get("token").toString();
        JSONObject jsonObject=JSONObject.fromObject(map.get("editCourseGroupInfo"));
        EditCourseGroupInfo editCourseGroupInfo=(EditCourseGroupInfo) JSONObject.toBean(jsonObject,EditCourseGroupInfo.class);
        //System.out.println(editCourseGroupInfo);
        //验证token是否正确
        if(tokenService.verifyToken(token,"院管理员")){
            if(courseGroupService.modifyCourseGroup(editCourseGroupInfo)){
                return new Result<>(1,"操作成功",null);
            }else{
                return new Result<>(0,"操作失败",null);
            }
        }else{
            return new Result<>(0,"没有操作权限,请登录",null);
        }
    }

    /**
     * 添加课程组信息
     * @param map 课程组信息和token
     * @return
     */
    @PostMapping("/add")
    public Result<Boolean> addCourseGroup(
            @RequestBody Map<String,Object> map
    ){
        String token=map.get("token").toString();
        JSONObject jsonObject=JSONObject.fromObject(map.get("addCourseGroupInfo"));
        AddCourseGroupInfo addcourseGroupInfo=(AddCourseGroupInfo) JSONObject.toBean(jsonObject,AddCourseGroupInfo.class);
        //System.out.println(addcourseGroupInfo);
        //验证token是否正确
        if(tokenService.verifyToken(token,"院管理员")){
            if(courseGroupService.addCourseGroup(addcourseGroupInfo)){
                return new Result<>(1,"操作成功",null);
            }else{
                return new Result<>(0,"操作失败",null);
            }
        }else{
            return new Result<>(0,"没有操作权限,请登录",null);
        }
    }

    /**
     * 获取详细的课程组信息
     * @param token token
     * @param courseGroupId 课程组ID
     * @return
     */
    @GetMapping("/get/courseGroupInfo")
    public Result<CourseGroupInfo> getCourseGroupInfo(
            @RequestParam("token")String token,
            @RequestParam("courseGroupId")String courseGroupId
    ){
        if(tokenService.verifyToken(token,"院管理员")){
            CourseGroupInfo courseGroupInfo=courseGroupService.getCourseGroupInfo(courseGroupId);
            return new Result<CourseGroupInfo>(1,"获取数据成功",courseGroupInfo);
        }else{
            return new Result<>(0,"没有操作权限,请登录",null);
        }
    }

    /**
     * 将老师添加进课程组
     * @param token token
     * @param courseGroupId 课程组ID
     * @param teacherId 老师ID
     * @return
     */
    @GetMapping("/select/teacher")
    public Result<Boolean> selectCourseGroupTeacher(
            @RequestParam("token")String token,
            @RequestParam("courseGroupId")String courseGroupId,
            @RequestParam("teacherId")String teacherId
    ){
        if(tokenService.verifyToken(token,"院管理员")){
            if(courseGroupService.operateCourseGroupTeacher(courseGroupId,teacherId,1)){
                return new Result<Boolean>(1,"操作成功",null);
            }else{
                return new Result<>(0,"操作失败",null);
            }
        }else{
            return new Result<>(0,"没有操作权限",null);
        }
    }

    /**
     * 将老师从课程组中删除
     * @param token token
     * @param courseGroupId 课程组ID
     * @param teacherId 老师ID
     * @return
     */
    @GetMapping("/deselect/teacher")
    public Result<Boolean> deselectCourseGroupTeacher(
            @RequestParam("token")String token,
            @RequestParam("courseGroupId")String courseGroupId,
            @RequestParam("teacherId")String teacherId
    ){
        if(tokenService.verifyToken(token,"院管理员")){
            if(courseGroupService.operateCourseGroupTeacher(courseGroupId,teacherId,0)){
                return new Result<Boolean>(1,"操作成功",null);
            }else{
                return new Result<>(0,"操作失败",null);
            }
        }else{
            return new Result<>(0,"没有操作权限",null);
        }
    }

    /**
     * 将班级添加进课程组
     * @param token token
     * @param courseGroupId 课程组ID
     * @param classId 班级ID
     * @return
     */
    @GetMapping("/select/class")
    public Result<Boolean> selectCourseGroupClass(
            @RequestParam("token")String token,
            @RequestParam("courseGroupId")String courseGroupId,
            @RequestParam("classId")String classId
    ){
        if(tokenService.verifyToken(token,"院管理员")){
            if(courseGroupService.operateCourseGroupClass(courseGroupId,classId,1)){
                return new Result<Boolean>(1,"操作成功",null);
            }else{
                return new Result<>(0,"操作失败",null);
            }
        }else{
            return new Result<>(0,"没有操作权限",null);
        }
    }

    /**
     * 将班级从课程组中删除
     * @param token token
     * @param courseGroupId 课程组ID
     * @param classId 班级ID
     * @return
     */
    @GetMapping("/deselect/class")
    public Result<Boolean> deselectCourseGroupClass(
            @RequestParam("token")String token,
            @RequestParam("courseGroupId")String courseGroupId,
            @RequestParam("classId")String classId
    ){
        if(tokenService.verifyToken(token,"院管理员")){
            if(courseGroupService.operateCourseGroupClass(courseGroupId,classId,0)){
                return new Result<Boolean>(1,"操作成功",null);
            }else{
                return new Result<>(0,"操作失败",null);
            }
        }else{
            return new Result<>(0,"没有操作权限",null);
        }
    }

}
